package com.zprmb.udzt;


import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.NumUtil;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.VolleryUtil;

public class V6BBtDetailsActivity extends BaseActivity implements OnClickListener{
	private TextView v6_bbt_product_time;
	private TextView v6_bbt_product_remainamount;
	private TextView v6_bbt_product_increaseNum;
	private TextView v6_bbt_product_minamount;
	private TextView v6_bbt_product_amount;
	private TextView v6_bbt_product_rate;
	private TextView v6_bbt_product_rateadd;
	private ProgressBar sk;
	private TextView tv_progress;
	private TextView is_redpackage;
	private LinearLayout li_project_info, li_project_protocol,
	li_project_record, li_project_problem;
	private String type,loanid;
	private String name;
	TimerTask timerTask;
	private Timer timer;
	long minute = 0;
	long second = 0;
	private ImageView img_share;
	private TextView tv_v4_productdetails_day;
	private TextView tv_v4_productdetails_hour;
	private TextView tv_v4_productdetails_minutes;
	private TextView tv_v4_productdetails_second;
	private Button btn_invest;
	private TextView tv_repaytype;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.v6bbt_detail_activity);
		setContentInview(this).setVisibility(View.INVISIBLE);
		BbtApplication.getInstance().addActvity(this);
		setImmersionStatus();
		initIntent();
		setTitle(name, this);
		setgoback(this);
		initview();
		initData();
		
	}

	private void initview() {
		// TODO Auto-generated method stub
    
		li_project_info=(LinearLayout)findViewById(R.id.li_project_info);
		li_project_protocol=(LinearLayout)findViewById(R.id.li_project_protocol);
		li_project_record=(LinearLayout)findViewById(R.id.li_project_record);
		li_project_problem=(LinearLayout)findViewById(R.id.li_project_problem);
		tv_v4_productdetails_day = (TextView) findViewById(R.id.tv_v4_productdetails_day);
		tv_v4_productdetails_hour = (TextView) findViewById(R.id.tv_v4_productdetails_hour);
		tv_v4_productdetails_minutes = (TextView) findViewById(R.id.tv_v4_productdetails_minutes);
		tv_v4_productdetails_second = (TextView) findViewById(R.id.tv_v4_productdetails_second);
		v6_bbt_product_amount=(TextView)findViewById(R.id.v6_bbt_product_amount);		
	    v6_bbt_product_remainamount=(TextView)findViewById(R.id.v6_bbt_product_remainamount);
		v6_bbt_product_minamount=(TextView)findViewById(R.id.v6_bbt_product_minamount);
		v6_bbt_product_rate=(TextView)findViewById(R.id.v6_bbt_product_rate);
		v6_bbt_product_rateadd=(TextView)findViewById(R.id.v6_bbt_product_rateadd);
		v6_bbt_product_time=(TextView)findViewById(R.id.v6_bbt_product_time);
		tv_progress=(TextView)findViewById(R.id.tv_progress);
		tv_repaytype=(TextView)findViewById(R.id.tv_repaytype);
		sk=(ProgressBar)findViewById(R.id.sk_progresss);
		btn_invest=(Button)findViewById(R.id.btn_invest);
		img_share=(ImageView)findViewById(R.id.img_share);
		btn_invest.setOnClickListener(this);
		img_share.setOnClickListener(this);
		li_project_info.setOnClickListener(new MyOnClickListener(0));
		li_project_protocol.setOnClickListener(new MyOnClickListener(1));
		li_project_record.setOnClickListener(new MyOnClickListener(2));
		li_project_problem.setOnClickListener(new MyOnClickListener(3));
	}
	private void initIntent() {
		// TODO Auto-generated method stub
		type=getIntent().getStringExtra("type");
		loanid=getIntent().getStringExtra("id");
		name=getIntent().getStringExtra("name");
	}
	/**  
     *      
     * 头标点击监听 3 */  
    private class MyOnClickListener implements OnClickListener{  
        private int index=0;  
        public MyOnClickListener(int i){  
            index=i;  
           
        }
        public void onClick(View v) {  
        	Intent intent=null;
        	 if(index==0){
             	intent =new Intent(V6BBtDetailsActivity.this,V4WebViewActivity.class);
             	intent.putExtra("url", "landing/bbtIntroForApp?"+"loanId="+loanid+"&number=0");
             	intent.putExtra("title", name);
//             	mExplosionField.explode(setContentInview2(V6DetailsActivity.this));
//             	setContentInview2(V6DetailsActivity.this).postDelayed(new Runnable() {
//					public void run() {
//						setContentInview2(V6DetailsActivity.this).setScaleX(1);
//						setContentInview2(V6DetailsActivity.this).setScaleY(1);
//						setContentInview2(V6DetailsActivity.this).setAlpha(255.0f);
//						setContentInview2(V6DetailsActivity.this).setVisibility(View.INVISIBLE);
////						iv_pic.setVisibility(View.GONE);
//					}
//				}, ExplosionAnimator.DEFAULT_DURATION);
//             	KShareViewActivityManager.getInstance(V6DetailsActivity.this).startActivity(V6DetailsActivity.this, V6InfoActivity.class, R.layout.details_top, R.layout.v6info_activity, li_content);
             	startActivity(intent);
             }else if(index==1){
            	 intent =new Intent(V6BBtDetailsActivity.this,V4WebViewActivity.class);
            	 intent.putExtra("url", "landing/bbtIntroForApp?"+"loanId="+loanid+"&number=1");
              	intent.putExtra("title", name);
              	startActivity(intent);
             }else if(index==2){
            	 intent =new Intent(V6BBtDetailsActivity.this,V4WebViewActivity.class);
            	 intent.putExtra("title", name);
            	 intent.putExtra("url", "landing/bbtIntroForApp?"+"loanId="+loanid+"&number=2");
              	startActivity(intent);
              }else if(index==3){
            	  intent =new Intent(V6BBtDetailsActivity.this,V4WebViewActivity.class);
            	  intent.putExtra("title", name);
            	
            	  intent.putExtra("url", "landing/bbtIntroForApp?"+"loanId="+loanid+"&number=3");
               	startActivity(intent);
              }
          
           
//            ToastUtil.showCenterToast(V6DetailsActivity.this, "点击");
        }  
          
    }  
    Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (msg.what == 0) {
				if (minute == 0) {
					if (second == 0) {

						if (timer != null) {
							timer.cancel();
							timer = null;
						}
						if (timerTask != null) {
							timerTask = null;
						}
					} else {
						second--;
						if (second >= 10) {
							// timeView.setText("0"+minute + ":" + second);
							tv_v4_productdetails_minutes.setText(minute + "");
							tv_v4_productdetails_second.setText(second + "");
						} else {
							// timeView.setText("0"+minute + ":0" + second);
							tv_v4_productdetails_minutes.setText("0" + minute);
							tv_v4_productdetails_second.setText("0" + second);
						}
					}
				} else {
					if (second == 0) {
						second = 59;
						minute--;
						if (minute >= 10) {
							// timeView.setText(minute + ":" + second);
							tv_v4_productdetails_minutes.setText(minute + "");
							tv_v4_productdetails_second.setText(second + "");
						} else {
							// timeView.setText("0"+minute + ":" + second);
							tv_v4_productdetails_minutes.setText("0" + minute);
							tv_v4_productdetails_second.setText(second + "");
						}
					} else {
						second--;
						if (second >= 10) {
							if (minute >= 10) {
								// timeView.setText(minute + ":" + second);
								tv_v4_productdetails_minutes.setText(minute
										+ "");
								tv_v4_productdetails_second
										.setText(second + "");
							} else {
								// timeView.setText("0"+minute + ":" + second);
								tv_v4_productdetails_minutes.setText("0"
										+ minute);
								tv_v4_productdetails_second
										.setText(second + "");
							}
						} else {
							if (minute >= 10) {
								// timeView.setText(minute + ":0" + second);
								tv_v4_productdetails_minutes.setText(minute
										+ "");
								tv_v4_productdetails_second.setText("0"
										+ second);
							} else {
								// timeView.setText("0"+minute + ":0" + second);
								tv_v4_productdetails_minutes.setText("0"
										+ minute);
								tv_v4_productdetails_second.setText("0"
										+ second);

							}
						}
					}
				}

			}
		}
	};
	
    private void initData(){
    	VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				LogUtils.e(V6BBtDetailsActivity.this, "散标"+jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					JSONObject json1 = jsonObject.optJSONObject("data");
					JSONObject json2 = json1.optJSONObject("loanRequest");
				setContentInview(V6BBtDetailsActivity.this).setVisibility(View.VISIBLE);
				sk.setProgress(Integer.parseInt(String.valueOf(
						json1.optDouble("progress") * 10).replace(".0", "")));
				tv_progress.setText(json1.optDouble("progress")+"%");
				v6_bbt_product_remainamount.setText(NumUtil.getQian(json1
						.optString("remainAmount")));
				v6_bbt_product_amount.setText(NumUtil.getQian(json1
						.optString("amount")));
				v6_bbt_product_time.setText(json1.optString("loanRealDays"));
				v6_bbt_product_minamount.setText(json2.optInt("minAmount")+"");
				if (json1.optInt("rateAdd") == 0) {
					v6_bbt_product_rate.setText(Regular.getDecimalFormatTwo(Double.parseDouble(json1
							.optString("originRate")) / 100) );
					
					v6_bbt_product_rateadd.setVisibility(View.GONE);
				} else {
					v6_bbt_product_rate.setText(Regular.getDecimalFormatTwo(Double.parseDouble(json1
							.optString("originRate")) / 100) );
					
					v6_bbt_product_rateadd.setVisibility(View.VISIBLE);
					v6_bbt_product_rateadd.setText("+"+Regular.getDecimalFormatOne(Double.parseDouble(json1
							.optString("rateAdd")) / 100) + "%");
				}
				tv_repaytype.setText(json1.optString("repayMethod")); 
//				ToJSON.initData2(jsonObject.toString(), "zhitou");
				Log.e("现在时间", json1.optLong("nowTime")+"");
				long now = json1.optLong("nowTime");
				long end = json1.optLong("endTime");
				long keyong = end - now;
				if (keyong > 0 && json1.optInt("remainAmount") > 0) {
//					btn.setBackgroundResource(R.drawable.my_button);
//					btn.setText("马上投资");
//					checkBox.setEnabled(true);
					long day = keyong / (24 * 60 * 60 * 1000);
					long hour = (keyong / (60 * 60 * 1000) - day * 24);
					long min = ((keyong / (60 * 1000)) - day * 24 * 60 - hour * 60);
					@SuppressWarnings("unused")
					long s = (keyong / 1000 - day * 24 * 60 * 60 - hour
							* 60 * 60 - min * 60);
					// long ms = (keyong - day * 24 * 60 * 60 * 1000 - hour*
					// 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
					// System.out.println(day + "天" + hour + "小时" + min +
					// "分"+ s + "秒" + ms + "毫秒");
					tv_v4_productdetails_day.setText(day + "");
					tv_v4_productdetails_hour.setText(hour + "");
					tv_v4_productdetails_minutes.setText(min + "");
					tv_v4_productdetails_second.setText("" + s);
					minute = min;
					second = s;
					timerTask = new TimerTask() {

						@Override
						public void run() {
							Message msg = new Message();
							msg.what = 0;
							handler.sendMessage(msg);
						}
					};
                    
                    	 if(timer==null){
                    		 
                    		 timer = new Timer();
                    		 timer.schedule(timerTask, 0, 1000);
                    	 }
			}else{
				tv_v4_productdetails_day.setText("0");
				tv_v4_productdetails_hour.setText("0");
				tv_v4_productdetails_minutes.setText("0");
				tv_v4_productdetails_second.setText("0");
			}
				
			}
			}
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(V6BBtDetailsActivity.this, Constant.URL
				+ "/apiLoan/loan/" + loanid, "");
    }
    private void setImmersionStatus() {
		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
			// 透明状态栏
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}
	}
    private void shareLogin() {
		VolleryUtil vollery = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if (jsonObject.optInt("success") == 1) {
					LogUtils.e(V6BBtDetailsActivity.this, jsonObject.toString());
					JSONObject json1 = jsonObject.optJSONObject("data");
//					Intent intent = new Intent(V6DetailsActivity.this,
//							WXEntryActivity.class);
//					intent.putExtra("sharelink", json1.optString("linkUrl"));
//					intent.putExtra("description",
//							json1.optString("description"));
//					intent.putExtra("title", json1.optString("title"));
//					intent.putExtra("imgurl", json1.optString("imageUrl"));
//					startActivity(intent);
//					LogUtils.e("url", Constant.URL + "/api/shareProductContent/"
//							+ type + "/" + id);
					share(V6BBtDetailsActivity.this, json1.optString("description"), json1.optString("title"), json1.optString("linkUrl"), json1.optString("imageUrl"));
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};

		vollery.setHeaders("token", getToken());
		vollery.setHeaders("client", "android");
		vollery.setShow(true);
		vollery.setRequestMethod(0);
		vollery.execute(V6BBtDetailsActivity.this, Constant.URL
				+ "/api/shareProductContent/" + type + "/" + loanid, "");
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_invest:
	Intent intent=new Intent(V6BBtDetailsActivity.this,V6BbtinvestActivity.class);
	intent.putExtra("name", name);	
	intent.putExtra("type", type);							
	intent.putExtra("id",loanid);
	startActivity(intent);
	break;
case R.id.img_share:
	shareLogin();
	break;
}
	}
}
